import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_admin/api/odoo/product_workreportrecord_odoo_api.dart';
import 'package:flutter_admin/api/odoo/stock_move_line_odoo_api.dart';
import 'package:flutter_admin/api/odoo/stock_quant_odoo_api.dart';
import 'package:flutter_admin/models/data_image_model.dart';
import 'package:flutter_admin/models/odoo/stock_quant.dart';
import 'package:flutter_admin/models/user_info.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/pages/large_screen/location_screen/data_image.dart';
import 'package:flutter_admin/utils/store_util.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'data_grid.dart';
import 'material_list.dart';

class LocationScreen extends StatefulWidget {
  LocationScreen({Key? key}) : super(key: key);

  @override
  _LocationScreenState createState() => _LocationScreenState();
}

class _LocationScreenState extends State<LocationScreen> {
  Map data1 = {'totalPrice':0,'stockData':[]};
  Map data2 = {'totalPrice':0,'stockData':[]};
  Map data3 = {'num':0,'data':[]};
  Map data4 = {'num':0,'data':[]};
  List<Map> data5 = [];
  List<Map> data6 = [];
  UserInfo userInfo = StoreUtil.getCurrentUserInfo();

  Duration duration = Duration(seconds: 60);
  late Timer _timer;
  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance!.addPostFrameCallback((c) {
      _loadData();
    });
    _timer = Timer.periodic(duration, (timer) {
      _loadData();
    });
  }

  @override
  void dispose() {
    _timer.cancel();
    super.dispose();
  }

  _loadData() async {
    // ResponseBodyApi res = await ProductWorkreportrecordOdooApi.getScreen(data: ['plant','=',userInfo.deptId]);
    ResponseBodyApi res1 = await StockMoveLineOdooApi.getLocation(userInfo.userId);
    if(!res1.success!||res1.data.length==0){
      return ;
    }
    ResponseBodyApi res = await StockQuantOdooApi.getScreen(data: [['location','=',res1.data[0]['locaiton_id']]]);
    if(res.success!){
      var data = res.data;
      data1 = {'totalPrice':data['location_price'][0]['location_price'],'stockData':data['location_price'][0]['stock_data']};
      data2 = {'totalPrice':data['retention_price'][0]['tarry_price'],'stockData':data['retention_price'][0]['tarry_data']};
      data3 = {'num':data['location_Out_data']['num'],'data':data['location_Out_data']['data']};
      data4 = {'num':data['location_In_data']['num'],'data':data['location_In_data']['data']};
      var data5_data =[];
      for(var element in data['price_date_stock']){
        data5_data.add({'x':element['date'],'y':element['price']});
      }
      data5 = [{'tabName':'总价值','data':data5_data}];
      var data6_data1 = [];
      var data6_data2 = [];
      for(var element in data['pick_date_count']){
        data6_data1.add({'x':element['date'],'y':element['OUT']});
        data6_data2.add({'x':element['date'],'y':element['IN']});
      }
      data6 = [{'tabName':'出库','data':data6_data1},{'tabName':'入库','data':data6_data2}];
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Center(
      child: Container(
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage("assets/bg/bg1.png"),
            fit: BoxFit.cover,
          ),
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Padding(
              padding: EdgeInsets.all(10.r),
              child: Text(
                "物料看板",
                style: TextStyle(
                    fontSize: 36.sp,
                    decoration: TextDecoration.none,
                    color: Color.fromRGBO(255, 255, 255, 1)),
              ),
            ),
            Expanded(
              child: Column(
                children: [
                  Expanded(
                    child: Row(
                      children: [
                        Expanded(child: MaterialList(dataMap: data1,title: '库房总价值',),),
                        Expanded(child: MaterialList(dataMap: data2,title: '呆滞物料总价值',),)
                      ],
                    )
                  ),
                  Expanded(
                    child: Row(
                      children: [
                        Expanded(child: DataGtid(dataList: data3['data'], title: '当天出库次数${data3["num"]}次',),),
                        Expanded(child: DataGtid(dataList: data4['data'], title: '当天入库次数${data4["num"]}次',),),
                      ],
                    ),
                  ),
                  Expanded(
                    child: Row(
                      children: [
                        Expanded(child: DataImage(dataList: data5, title: '库房总价值',),),
                        Expanded(child: DataImage(dataList: data6, title: '出入库次数',),),
                      ],
                    ),
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}
